﻿var FSGY=function () {
   var  FSGYObj={
     fThreshold : 0.7,//验证门限
     strTipsType: "StartReadCard",
    timerFlag1:"",//提示牌定时器标志
   timerFlag2:"",//身份证阅读定时器标志
    bFIInit:false,//人脸模块初始化标志
    bRCInit:false,//身份证模块初始化标志
    bIsGrayVideoInit:false,//控制活体检测模式
    bTipFlash:true,//控制提示是否闪烁
     FLFICtrl:{}
   };


  FSGYObj.OperationTips=function()  {
    var docTips = document.getElementById("idTips")
    docTips.style.fontSize = 50+'px';
    if(FSGYObj.strTipsType == "StartReadCard")
    {
      docTips.style.color = "#4169E1";
      if(FSGYObj.bTipFlash)
      {
        docTips.innerHTML = "请刷身份证";
        FSGYObj.bTipFlash = !FSGYObj.bTipFlash;
      }else{
        docTips.innerHTML = "";
        FSGYObj.bTipFlash = !FSGYObj.bTipFlash;
      }
    }else if(FSGYObj.strTipsType == "ReadCardSuccess")
    {
      docTips.style.color = "#00FF00";
      docTips.innerHTML = "读卡成功";
    }else if(FSGYObj.strTipsType == "ReadCardFail")
    {
      docTips.style.color = "#FF0000";
      docTips.innerHTML = "读卡失败";
    }else if(FSGYObj.strTipsType == "FaceIdentifySuccess")
    {
      docTips.style.color = "#00FF00";
      docTips.innerHTML = "人证比对通过，5秒后复位";
    }else if(FSGYObj.strTipsType == "IndentifyFail")
    {
      docTips.style.color = "#FF0000";
      docTips.innerHTML = "验证失败，5秒后复位";
    }else if(FSGYObj.strTipsType == "GetPhotoFaceFail")
    {
      docTips.style.color = "#FF0000";
      docTips.innerHTML = "未检测到证件照片人脸，5秒后复位";

    }else if(FSGYObj.strTipsType == "GetPhotoFaceFeatureFail")
    {
      docTips.style.color = "#FF0000";
      docTips.innerHTML = "提取证件照片人脸特征失败，5秒后复位";
    }else if(FSGYObj.strTipsType == "GetVideoFaceFail")
    {
      docTips.style.color = "#FF0000";
      docTips.innerHTML = "验证失败，视频中未检测到人脸，5秒后复位";
    }else if(FSGYObj.strTipsType == "GettVideoFaceFeatureFail")
    {
      docTips.style.color = "#FF0000";
      docTips.innerHTML = "验证失败，视频中人脸特征提取失败，5秒后复位";
    }else if(FSGYObj.strTipsType == "LoadPhotoFail")
    {
      docTips.style.color = "#FF0000";
      docTips.innerHTML = "加载证件照片失败，5秒后复位";
    }else if(strTipsType == "DetectingVideoFace")
    {
      docTips.style.color = "#FF0000";
      if(FSGYObj.bTipFlash)
      {
        docTips.innerHTML = "视频未发现人脸，请调整角度正对摄像头";
        FSGYObj.bTipFlash = !FSGYObj.bTipFlash;
      }else{
        docTips.innerHTML = "";
        FSGYObj.bTipFlash = !FSGYObj.bTipFlash;
      }
    }else if(FSGYObj.strTipsType == "GettingVideoFaceFeature")
    {
      docTips.style.color = "#FF0000";
      if(FSGYObj.bTipFlash)
      {
        docTips.innerHTML = "视频提取人脸特征失败，请保持摄像头清晰";
        FSGYObj.bTipFlash = !FSGYObj.bTipFlash;
      }else{
        docTips.innerHTML = "";
        FSGYObj.bTipFlash = !FSGYObj.bTipFlash;
      }
    }else if(FSGYObj.strTipsType == "IndentifyingFail")
    {
      docTips.style.color = "#FF0000";
      if(FSGYObj.bTipFlash)
      {
        docTips.innerHTML = "比对中，请调整姿势正对摄像头";
        FSGYObj.bTipFlash = !FSGYObj.bTipFlash;
      }else{
        docTips.innerHTML = "";
        FSGYObj.bTipFlash = !FSGYObj.bTipFlash;
      }
    }else if(FSGYObj.strTipsType == "StopDetectFace")
    {
      docTips.style.color = "#FF0000";
      docTips.innerHTML = "停止人脸检测";
    }
  }



  FSGYObj.AutoReadCard=function() {
    if(!FSGYObj.bRCInit)
    {
      return;
    }
    var nRet = FLFICtrl.FLRC_ReadCard();
    if(nRet == 1)
    {
      //读卡成功，清除读卡定时器
      clearInterval(FSGYObj.timerFlag2);

      document.getElementById("idIdPhoto").src = FLFICtrl.AppPath+"zp.bmp";
      // alert('src'+FLFICtrl.AppPath+"zp.bmp");
      document.getElementById("idName").value = FLFICtrl.FLRC_GetValue("Name");
      document.getElementById("idIdNum").value = FLFICtrl.FLRC_GetValue("IdNum");
      document.getElementById("idAddress").value = FLFICtrl.FLRC_GetValue("Address");
      document.getElementById("idUseLife").value = FLFICtrl.FLRC_GetValue("UseLifeStart")+"-"+FLFICtrl.FLRC_GetValue("UseLifeEnd");
      FSGYObj.strTipsType = "ReadCardSuccess";
      document.getElementById("idReadCardStatus").value = "读卡成功";

      FSGYObj.FIStartIdentifyFace();
    }else if(nRet == 0)
    {
      //strTipsType = "ReadCardFail";
      document.getElementById("idReadCardStatus").value = "读卡失败";
    }
  }

  FSGYObj.FIInit=function ()
  {

    /*int FLFI_Init(int nModel);
          nModel:要初始化的模式 1为只有彩色视频 2为彩色红外双目视频
          返回值：1 单目模式成功
                  2 双目模式成功
                  0 加载flscan.dll失败
                  -1 彩色镜头初始化失败
                  -2 彩色镜头初始化成功，红外镜头初始化失败,人脸模块成功
                  -3 人脸模块初始化失败
        */
    // FSGYObj.FLFICtrl= document.getElementById("FLFICtrl");
    // FSGYObj.FLFICtrl
    var nRet1 = FLFICtrl.FLFI_Init(2);

    var strRes = "";
    if(nRet1 == 1)
    {
      strRes = "人脸模块初始化成功，";
      FSGYObj.bFIInit = true;
      FSGYObj.bIsGrayVideoInit = false;
    }else if(nRet1 == -3 || nRet1 == -1 || nRet1 == 0)
    {
      strRes = "人脸模块初始化失败，";
      FSGYObj.bFIInit = false;
      FSGYObj.bIsGrayVideoInit = false;
    }else if(nRet1 == -2)
    {
      strRes = "人脸模块初始化成功，";
      FSGYObj.bFIInit = true;
      FSGYObj.bIsGrayVideoInit = false;
    }else if(nRet1 == 2)
    {
      strRes = "人脸模块初始化成功，";
      FSGYObj.bFIInit = true;
      FSGYObj.bIsGrayVideoInit = true;
    }
    var nRet2 = FLFICtrl.FLRC_Init();
    if(nRet2 == 1)
    {
      strRes += "身份证阅读模块初始化成功";
      document.getElementById("idInitStatus").value = strRes;
      FSGYObj.bRCInit = true;
    }else if(nRet2 == 0)
    {
      strRes += "身份证阅读模块初始化失败";
      document.getElementById("idInitStatus").value = strRes;
    }


  }

  FSGYObj.FIOpenVideo=function ()  {
      if(!FSGYObj.bFIInit)
      {
        return;
      }
      var nRet;
      if(FSGYObj.bIsGrayVideoInit == false)
      {
        nRet = FLFICtrl.FLFI_OpenVideo(1);
      }else{
        nRet = FLFICtrl.FLFI_OpenVideo(2);
      }

      if(nRet == 1)
      {
        //alert("打开视频成功！");
        document.getElementById("idOpenVideoStatus").value = "打开视频成功";

      }else if(nRet == 0)
      {
        //alert("打开视频失败！");
        document.getElementById("idOpenVideoStatus").value = "打开视频失败";
      }



  }

  FSGYObj.FIStartDetectFace=function ()
  {
    if(! FSGYObj.bFIInit)
    {
      return;
    }
    var nRet = FLFICtrl.FLFI_StartDetectFace(1,1,0);
    var strRes = "";
    if(nRet == 1)
    {
      strRes = "视频1开启检测成功，";
    }else if(nRet == 0)
    {
      strRes = "视频1开启检测失败，";
    }

    if( FSGYObj.bIsGrayVideoInit)
    {
      nRet = FLFICtrl.FLFI_StartDetectFace(2,1,1);
      if(nRet == 1)
      {
        strRes += "视频2开启检测成功";
        document.getElementById("idStartDetectStatus").value = strRes;
      }else if(nRet == 0)
      {
        strRes += "视频2开启检测失败";
        document.getElementById("idStartDetectStatus").value = strRes;
      }
    }
    //开启提示定时器跟读卡定时器
    if( FSGYObj.bFIInit &&  FSGYObj.bRCInit)
    {
      clearInterval(FSGYObj.timerFlag1);
      clearInterval(FSGYObj.timerFlag2);
      FSGYObj.strTipsType = "StartReadCard";
  /*    FSGYObj.timerFlag1 = setInterval("FSGYObj.OperationTips()",500);
      FSGYObj.timerFlag2 = setInterval("FSGYObj.AutoReadCard()",500);*/
   /*   FSGYObj.timerFlag1 = setInterval("FSGYObj.OperationTips()",5000);
      FSGYObj.timerFlag2 = setInterval("FSGYObj.AutoReadCard()",5000);*/
      FSGYObj.OperationTips();
      FSGYObj.AutoReadCard();
    }
  }

  FSGYObj.FIStartIdentifyFace= function ()
  {
    if(! FSGYObj.bFIInit)
    {
      return;
    }
    var nRet;
    if( FSGYObj.bIsGrayVideoInit)
    {
      nRet = FLFICtrl.FLFI_StartIdentifyThread(FLFICtrl.AppPath + "zp.bmp",FSGYObj.fThreshold,0,1,5,"");
    }else{
      nRet = FLFICtrl.FLFI_StartIdentifyThread(FLFICtrl.AppPath + "zp.bmp",FSGYObj.fThreshold,0,2,5,"");
    }

    if(nRet == 1)
    {
      document.getElementById("idStartIdentifyStatus").value = "开启比对成功";
      //比对结果
      FSGYObj.check();

    }else if(nRet == 0)
    {
      document.getElementById("idStartIdentifyStatus").value = "开启比对失败";
    }
  }
  //检测是否验证通过
  FSGYObj.check=function(){
    var fFaceScore = FLFICtrl.FaceScore.toFixed(6);
    var fColorLiveScore = FLFICtrl.ColorLiveScore.toFixed(6);
    var fGrayLiveScore = FLFICtrl.GrayLiveScore.toFixed(6);
    var nVerifyStatus = FLFICtrl.VerifyStatus;

    var FaceScoreValue;
    if(fFaceScore == -10001)
    {
      FaceScoreValue = "未加载到照片";
    }else if(fFaceScore == -10002)
    {
      FaceScoreValue = "未检测到照片人脸";
    }else if(fFaceScore == -10003)
    {
      FaceScoreValue = "未提取到照片人脸特征";
    }else if(fFaceScore == -20001)
    {
      FaceScoreValue = "未检测到视频人脸";
    }else if(fFaceScore == -20002)
    {
      FaceScoreValue = "未提取到视频人脸特征";
    }else{
      FaceScoreValue = fFaceScore;
    }

    var ColorLiveScoreValue;
    if(fColorLiveScore == -10001)
    {
      ColorLiveScoreValue = "未加载到照片";
    }else if(fColorLiveScore == -10002)
    {
      ColorLiveScoreValue = "未检测到照片人脸";
    }else if(fColorLiveScore == -10003)
    {
      ColorLiveScoreValue = "未提取到照片人脸特征";
    }else if(fColorLiveScore == -20001)
    {
      ColorLiveScoreValue = "未检测到视频人脸";
    }else if(fColorLiveScore == -20002)
    {
      ColorLiveScoreValue = "未提取到视频人脸特征";
    }else if(fColorLiveScore == -1)
    {
      ColorLiveScoreValue = "未开启";
    }else{
      ColorLiveScoreValue = fColorLiveScore;
    }

    var GrayLiveScoreValue;
    if(fGrayLiveScore == -10001)
    {
      GrayLiveScoreValue = "未加载到照片";
    }else if(fGrayLiveScore == -10002)
    {
      GrayLiveScoreValue = "未检测到照片人脸";
    }else if(fGrayLiveScore == -10003)
    {
      GrayLiveScoreValue = "未提取到照片人脸特征";
    }else if(fGrayLiveScore == -20001)
    {
      GrayLiveScoreValue = "未检测到视频人脸";
    }else if(fGrayLiveScore == -20002)
    {
      GrayLiveScoreValue = "未提取到视频人脸特征";
    }else if(fGrayLiveScore == -1)
    {
      GrayLiveScoreValue = "未开启";
    }else{
      GrayLiveScoreValue = fGrayLiveScore;
    }

    document.getElementById("idColorIdfyScore").value = FaceScoreValue;
    document.getElementById("idColorLiveScore").value = ColorLiveScoreValue;
    document.getElementById("idGrayLiveScore").value = GrayLiveScoreValue;

    if(nVerifyStatus == 0)//在验证中
    {
      if(fFaceScore == -20001)
      {
        FSGYObj.strTipsType = "DetectingVideoFace";//在验证中视频未发现人脸
      }else if(fFaceScore == -20002)
      {
        FSGYObj.strTipsType = "GettingVideoFaceFeature";//在验证中视频提取人脸特征失败
      }else{
        FSGYObj.strTipsType = "IndentifyingFail";//在验证中分数不够或者活体未通过
      }
    }else if(nVerifyStatus == 1)//验证通过
    {
      alert('人证验证通过');
      FSGYObj.strTipsType = "FaceIdentifySuccess";
      setTimeout(function(){
        FSGYObj.strTipsType = "StartReadCard";
        FSGYObj.ClearInfo();
        clearInterval(FSGYObj.timerFlag2);
        FSGYObj.timerFlag2 = setInterval("AutoReadCard()",500);
      },5000);
    }else if(nVerifyStatus == -1)//验证失败
    {
      if(fFaceScore == -10001)
      {
        FSGYObj.strTipsType = "LoadPhotoFail";//加载证件照片失败验证失败
      }else if(fFaceScore == -10002)
      {
        FSGYObj.strTipsType = "GetPhotoFaceFail";//验证失败照片没找到人脸
      }else if(fFaceScore == -10003)
      {
        FSGYObj.strTipsType = "GetPhotoFaceFeatureFail";//验证失败照片人脸特征提取失败
      }else if(fFaceScore == -20001)
      {
        FSGYObj.strTipsType = "GetVideoFaceFail";//验证失败视频中没找到人脸
      }else if(fFaceScore == -20002)
      {
        FSGYObj.strTipsType = "GettVideoFaceFeatureFail";//验证失败视频提取人脸特征失败
      }else{
        FSGYObj.strTipsType = "IndentifyFail";//验证失败分数不够或者活体未通过
      }
      //复位
      setTimeout(function(){
        FSGYObj.strTipsType = "StartReadCard";
        FSGYObj.ClearInfo();
        clearInterval(FSGYObj.timerFlag2);
        FSGYObj.timerFlag2 = setInterval(FSGYObj.AutoReadCard(),500);
      },5000);
    }
  }
//停止人脸检测
  FSGYObj.FIStopDetectFace_Photo= function ()
  {

    if(! FSGYObj.bFIInit)
    {
      return;
    }
    //停止读卡定时器,不然停止检测人脸后读卡后进行人脸比对会出问题
    if( FSGYObj.bFIInit &&  FSGYObj.bRCInit)
    {
      clearInterval(FSGYObj.timerFlag2);
      FSGYObj.strTipsType = "StopDetectFace";
    }

    var nRet = FLFICtrl.FLFI_StopDetectFace(1);
    var strRes = "";
    if(nRet == 1)
    {
      strRes = "视频1停止检测成功，";
    }else if(nRet == 0)
    {
      strRes = "视频1停止检测失败，";
    }

    if(FSGYObj.bIsGrayVideoInit)
    {
      nRet = FLFICtrl.FLFI_StopDetectFace(2);
      if(nRet == 1)
      {
        strRes += "视频2停止检测成功";
        document.getElementById("idCloseDetectStatus").value = strRes;
      }else if(nRet == 0)
      {
        strRes += "视频2停止检测失败";
        document.getElementById("idCloseDetectStatus").value = strRes;
      }
    }
  }

  FSGYObj.FIUnInit=function ()
  {
    if(!FSGYObj.bFIInit && !FSGYObj.bRCInit)
    {
      return;
    }

    clearInterval(FSGYObj.timerFlag1);
    clearInterval(FSGYObj.timerFlag2);
    var nRet;
    if(!FSGYObj.bIsGrayVideoInit)
    {
      nRet = FLFICtrl.FLFI_UnInit(1);
    }else{
      nRet = FLFICtrl.FLFI_UnInit(2);
    }

    var strRes = "";
    if(nRet == 1)
    {
      FSGYObj.bFIInit = false;
      strRes = "人脸模块反初始化成功，";
    }else if(nRet == 0)
    {
      strRes = "人脸模块反初始化失败，";
    }
    nRet = FLFICtrl.FLRC_UnInit();
    if(nRet == 1)
    {
      FSGYObj.bRCInit = false;
      strRes += "身份证阅读模块反初始化成功";
      document.getElementById("idUnInitStatus").value = strRes;
    }else if(nRet == 0)
    {
      strRes += "身份证阅读模块反初始化失败";
      document.getElementById("idUnInitStatus").value = strRes;
    }
  }

  FSGYObj.FIReadCard_C1=function ()
  {
    if(! FSGYObj.bRCInit)
    {
      return;
    }
    var nRet = FLFICtrl.FLRC_ReadCard();
    if(nRet == 1)
    {
      document.getElementById("idIdPhoto").src = FLFICtrl.AppPath+"zp.bmp";
      document.getElementById("idName").value = FLFICtrl.FLRC_GetValue("Name");
      document.getElementById("idIdNum").value = FLFICtrl.FLRC_GetValue("IdNum");
      document.getElementById("idAddress").value = FLFICtrl.FLRC_GetValue("Address");
      document.getElementById("idUseLife").value = FLFICtrl.FLRC_GetValue("UseLifeStart")+"-"+FLFICtrl.FLRC_GetValue("UseLifeEnd");
      FSGYObj.strTipsType = "ReadCardSuccess";
      document.getElementById("idReadCardStatus").value = "读卡成功";
    }else if(nRet == 0)
    {
      FSGYObj.strTipsType = "ReadCardFail";
      document.getElementById("idReadCardStatus").value = "读卡失败";
      // alert('读卡失败');
    }
  }

  FSGYObj.ClearInfo=function (){
    document.getElementById("idIdPhoto").src = "";
    document.getElementById("idName").value = "";
    document.getElementById("idIdNum").value = "";
    document.getElementById("idAddress").value = "";
    document.getElementById("idUseLife").value = "";
    document.getElementById("idColorIdfyScore").value = "";
    document.getElementById("idColorLiveScore").value = "";
    document.getElementById("idGrayLiveScore").value = "";
  }
  return FSGYObj;

}();
export default FSGY;
